package com.tenbit.tbw.ctl;



import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.tenbit.tbw.svc.EMP1010SVC;
import com.tenbit.tbw.vo.EMP1020VO;
import com.tenbit.tbw.vo.EMP1030VO;
import com.tenbit.tbw.vo.EMP1040VO;

@Controller
public class EMP1010CTL
{
	@Autowired
	private EMP1010SVC svc;
	
	@RequestMapping("EMP1010M0.do")
	public ModelAndView getCodeBookList()
	{
		ModelAndView mav = new ModelAndView();
		mav.setViewName("1st_week/EMP1010M0");
		mav.addObject("model", svc.getEmployeeCodebook());
		return mav;
	}
	
	@RequestMapping("EMP1010M1.do")
	public ModelAndView getEemployeeList(HttpServletRequest request,HttpServletResponse Response)
	{
		ModelAndView mav = new ModelAndView();
		int page=1;
		int limit=10;
		if(request.getParameter("page") != null)
		{
			page = Integer.parseInt(request.getParameter("page"));
		}
		
		int listcount= svc.getTotalNum();
		int maxpage = (int)((double)listcount/limit+0.95);
		int startpage =(((int)((double)page / 10+0.9))-1)*10+1;
		
		mav.setViewName("1st_week/EMP1010M1");
		mav.addObject("employeeList", svc.getEmployee(page,limit));
		mav.addObject("page", page);
		mav.addObject("totalNum", listcount);
		mav.addObject("startpage", startpage);
		mav.addObject("maxpage", maxpage);
		mav.addObject("model", svc.getEmployeeCodebook());
		return mav;
	}
	@RequestMapping("EMP1010M2.do")
	public ModelAndView getSearchList(HttpServletRequest request,HttpServletResponse Response)
	{
		ModelAndView mav = new ModelAndView();
		Enumeration<String> names = request.getParameterNames();
		Map<String, Object> map = new HashMap<String, Object>();
		while(names.hasMoreElements()){
			String name = names.nextElement();
			map.put(name, request.getParameter(name));
		}
		String[] skillCode = request.getParameterValues("skillCode");
		String[] graduationTypeCode=request.getParameterValues("graduationTypeCode");
			
		String month1 =request.getParameter("month1");
		String day1 = request.getParameter("day1");
		String month2 =request.getParameter("month2");
		String day2 = request.getParameter("day2");
		
		month1=month1.trim();
		day1=day1.trim();
		month2=month2.trim();
		day2=day2.trim();
		
		map.remove("month1");
		map.remove("day1");
		map.remove("month2");
		map.remove("day2");
		
		map.put("month1", month1);
		map.put("day1", day1);
		map.put("month2", month2);
		map.put("day2", day2);
		
		map.put("skillCode", skillCode);
		map.put("graduationTypeCode", graduationTypeCode);

		if(month1.equals("monthDefault1")){
		month1="1";
		}
		if(day1.equals("dayDefault1")){
			day1="1";
		}
		
		if(month2.equals("monthDefault2")){
			month2="12";
		}
		
		if(day2.equals("dayDefault2")){
			day2="28";
		}
		
		if(Integer.parseInt(month1)<10){
			month1="0"+month1;
		}
		
		if(Integer.parseInt(day1)<10){
			day1="0"+day1;
		}
		if(Integer.parseInt(month2)<10){
			month2="0"+month2;
		}
		
		if(Integer.parseInt(day2)<10){
			day2="0"+day2;
		}
		
		
		String graduationDate1 = map.get("year1")+"-"+month1+"-"+day1;
		String graduationDate2 = map.get("year2")+"-"+month2+"-"+day2;
	
		map.put("graduationDate1", graduationDate1);
		map.put("graduationDate2", graduationDate2);
					
		int page=1;
		int limit=10;
		if(request.getParameter("page") != null)
		{
			page = Integer.parseInt(request.getParameter("page"));
		}
		
		int listcount=0;
		
		String msg = null;
		if(svc.getSearchTotalNum(map)==null){
			msg="졸업일의 검색조건이 잘못되었습니다.";
			mav.setViewName("1st_week/EMP1010M2");
			mav.addObject("map",map);
			mav.addObject("model", svc.getEmployeeCodebook());
			mav.addObject("msg",msg);
			return mav;
		} else {
			listcount=(Integer) svc.getSearchTotalNum(map);
		}
		
		List<EMP1020VO> list = null;
		if(svc.getSearchList(page,limit,map)==null){
			msg="졸업일의 검색조건이 잘못되었습니다.";
			mav.setViewName("1st_week/EMP1010M2");
			mav.addObject("map",map);
			mav.addObject("model", svc.getEmployeeCodebook());
			mav.addObject("msg",msg);
			return mav;
		} else {
			list=svc.getSearchList(page,limit,map);
		}
		
		int maxpage = (int)((double)listcount/limit+0.95);
		int startpage =(((int)((double)page / 10+0.9))-1)*10+1;
		mav.addObject("map",map );
		
		mav.addObject("searchList",list );
		mav.addObject("totalNum", listcount);
		mav.addObject("page", page);
		mav.addObject("startpage", startpage);
		mav.addObject("maxpage", maxpage);
		mav.addObject("model", svc.getEmployeeCodebook());
		mav.setViewName("1st_week/EMP1010M2");
		return mav;
	}
	
	@RequestMapping(value="EMP1010P1.do", method=RequestMethod.GET)
	public ModelAndView getJoinPopUp (HttpServletRequest request,HttpServletResponse Response)
	{
		ModelAndView mav = new ModelAndView();
		mav.setViewName("1st_week/EMP1010P1");
		mav.addObject("model", svc.getEmployeeCodebook());
		return mav;
		
	}	
	
	@RequestMapping("EMP1010P2.do")
	public ModelAndView getModifyEmployeeInfo (HttpServletRequest request,HttpServletResponse Response)
	{
		ModelAndView mav = new ModelAndView();
		
		String empCode = request.getParameter("empCode");
		
		System.out.println(empCode);
		
		List<EMP1030VO> empInfo=null;
		List<EMP1040VO> skills=null;
		String msg=null;
		
		skills=svc.getSkills(empCode);
		empInfo=svc.getEmployeeInfo(empCode);
		
		String[] idNum=empInfo.get(0).getIdNmber().split("-");
		
		String idNumber1=idNum[0];
		String idNumber2=idNum[1];
		
		String[] gDate = empInfo.get(0).getGraduationDate().split("-");
		int year=  Integer.parseInt(gDate[0]);
		int month= Integer.parseInt(gDate[1]);
		int day= Integer.parseInt(gDate[2]);
		
		mav.addObject("year",year);
		mav.addObject("month",month);
		mav.addObject("day",day);
		mav.addObject("idNumber1",idNumber1);
		mav.addObject("idNumber2",idNumber2);
		mav.addObject("skills", skills);
		mav.addObject("empInfo", empInfo);
		mav.addObject("msg",msg);
		mav.setViewName("1st_week/EMP1010P2");
		mav.addObject("model", svc.getEmployeeCodebook());
		return mav;
	}
	
	@RequestMapping("EMP1010P21.do")
	public ModelAndView setEmployeeInfo(HttpServletRequest request,HttpServletResponse Response)
	{
		ModelAndView mav = new ModelAndView();
		Enumeration<String> names = request.getParameterNames();
		Map<String, Object> map = new HashMap<String, Object>();
		while(names.hasMoreElements()){
			String name = names.nextElement();
			map.put(name, request.getParameter(name));
		}
		
		String msg1=null;
		String empCode=(String) map.get("employeeCode");
		
		System.out.println(empCode);
		
		String[] skillCode = request.getParameterValues("skillCode");
		String graduationDate = map.get("year1")+"-"+map.get("month1")+"-"+map.get("day1");
		String idNumber=map.get("idNum1")+"-"+map.get("idNum2");
		
		String key=(String) map.get("idNum2");
		if(key.length()<7){
				msg1="입력된 정보가 잘못되었습니다.";
				mav.setViewName("1st_week/EMP1010P2");
				mav.addObject("msg",msg1);
				mav.addObject("map",map);
				mav.addObject("empCode",empCode);
				mav.addObject("model", svc.getEmployeeCodebook());
				return mav;
				
		}
		
		int keyNum=Integer.parseInt(key.substring(0,1));
		String genderCode=null;
		
		if(keyNum==1 || keyNum==3){
			genderCode="MALE";
		} else if(keyNum==2|| keyNum==4){
			genderCode="FEMALE";
		}
		
		map.put("genderCode", genderCode);
		map.put("idNumber", idNumber);
		map.put("graduationDate", graduationDate);
		map.put("skillCode", skillCode);
		
		boolean result=svc.setEmployeeInfo(map);
		
		
		if(result!=true){
			msg1="입력된 정보가 잘못 되었습니다.";
			mav.setViewName("1st_week/EMP1010P2");
			mav.addObject("empCode",empCode);
			mav.addObject("msg",msg1);
			mav.addObject("map",map);
			mav.addObject("model", svc.getEmployeeCodebook());
			return mav;
		}
		msg1="정상적으로 수정되었습니다.";
		
		mav.addObject("msg",msg1);
		mav.addObject("empCode",empCode);
		mav.addObject("map",map);
		mav.setViewName("1st_week/EMP1010P2");
		mav.addObject("model", svc.getEmployeeCodebook());
		return mav;
	}
	@RequestMapping("EMP1010P22.do")
	public ModelAndView deleteEmployeeInfo(HttpServletRequest request,HttpServletResponse Response)
	{
		ModelAndView mav = new ModelAndView();
		int empCode = Integer.parseInt(request.getParameter("employeeCode"));
		
		boolean result=svc.deleteEmployeeInfo(empCode);
		String msg=null;
		if(result!=true){
			msg="삭제되지 않았습니다.";
			mav.setViewName("1st_week/EMP1010P2");
			mav.addObject("empCode",empCode);
			mav.addObject("msg",msg);
			mav.addObject("model", svc.getEmployeeCodebook());
			return mav;
		}
	
		msg="정상적으로 삭제되었습니다.";
		mav.setViewName("1st_week/EMP1010P2");
		mav.addObject("empCode",empCode);
		mav.addObject("msg",msg);
		mav.addObject("model", svc.getEmployeeCodebook());
		return mav;
	}
	
	
	@RequestMapping("EMP1010P11.do")
	public ModelAndView insertEmployee (HttpServletRequest request,HttpServletResponse Response)
	{
		ModelAndView mav = new ModelAndView();
		


		Enumeration<String> names = request.getParameterNames();
		Map<String, Object> map = new HashMap<String, Object>();
		while(names.hasMoreElements()){
			String name = names.nextElement();
			map.put(name, request.getParameter(name));
		}
		String msg=null;
		
		String[] skillCode = request.getParameterValues("skillCode");
		String graduationDate = map.get("year1")+"-"+map.get("month1")+"-"+map.get("day1");
		String idNumber=map.get("idNum1")+"-"+map.get("idNum2");
		
		
		String key=(String) map.get("idNum2");
		if(key.length()<7){
			
			System.out.println(14);
			
				msg="입력된 정보가 잘못되었습니다.";
				mav.setViewName("1st_week/EMP1010P1");
				mav.addObject("msg",msg);
				mav.addObject("model", svc.getEmployeeCodebook());
				return mav;
				
		}
		
		int keyNum=Integer.parseInt(key.substring(0,1));
		String genderCode=null;
		
		if(keyNum==1 || keyNum==3){
			genderCode="M";
		} else if(keyNum==2|| keyNum==4){
			genderCode="F";
		}
		map.put("genderCode", genderCode);
		map.put("idNumber", idNumber);
		map.put("graduationDate", graduationDate);
		map.put("skillCode", skillCode);
		
		System.out.println(map);
		boolean result=svc.insertEmployeeInfo(map);
		
		System.out.println(result);
		
		if(result!=true){
			msg="입력된 정보가 잘못되었습니다.";
			mav.setViewName("1st_week/EMP1010P1");
			mav.addObject("msg",msg);
			mav.addObject("model", svc.getEmployeeCodebook());
			return mav;
		}
		msg="정상적으로 등록 되었습니다.";
		mav.setViewName("1st_week/EMP1010P1");
		mav.addObject("msg",msg);
		mav.addObject("model", svc.getEmployeeCodebook());
		return mav;
	}	
}
